package com.xhwl.centiotdata.suppliersystem.vehicle.DaHuaIcc;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.pojo.constant.DaHuaIccConstant;
import com.xhwl.centiotdata.pojo.dto.dahua.VehicleInOutRecordDaHuaDTO;
import com.xhwl.centiotdata.suppliersystem.dahuaIcc.GeneralDaHuaIccHandler;
import com.xhwl.common.pojo.cent.vehicle.bo.config.VehicleIntegrationConfigBO;
import com.xhwl.common.pojo.cent.vehicle.bo.http.query.QueryAccessRecordBO;
import com.xhwl.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * 车行对接大华icc的实现逻辑类，即处理程序
 */
@Slf4j
@Component
public class DaHuaIccHandler {

    @Autowired
    private GeneralDaHuaIccHandler generalDaHuaIccHandler;

    /**
     * 获取记录
     * @param vehicleIntegrationConfigBO
     * @return
     */
    public List<VehicleInOutRecordDaHuaDTO> pullRecord(VehicleIntegrationConfigBO vehicleIntegrationConfigBO, QueryAccessRecordBO queryAccessRecordBO){
        LinkedList<VehicleInOutRecordDaHuaDTO> vehicleInOutRecordDaHuaDTOS = new LinkedList<>();
        Integer pageNum = 1;
        List<VehicleInOutRecordDaHuaDTO> recordList = null;
        do {
            recordList = this.getAccessRecord(vehicleIntegrationConfigBO,pageNum, queryAccessRecordBO);
            if (CollectionUtils.isEmpty(recordList)) {
                break;
            }
            vehicleInOutRecordDaHuaDTOS.addAll(recordList);
            ++pageNum;
        }while (!CollectionUtils.isEmpty(recordList) && recordList.size() == 500);
        return vehicleInOutRecordDaHuaDTOS;
    }

    /**
     * 获取出行记录
     */
    private List<VehicleInOutRecordDaHuaDTO> getAccessRecord(VehicleIntegrationConfigBO vehicleIntegrationConfigBO, Integer pageNum, QueryAccessRecordBO queryAccessRecordBO){
        String url = "/evo-apigw/ipms/caraccess/find/his";
        HashMap<String, Object> params = new HashMap<>();
        params.put("pageNum", pageNum);
        params.put("pageSize", 500);

        if (queryAccessRecordBO.getStatus()==0){
            params.put("enterTimeStrLeft",
                    new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(queryAccessRecordBO.getStartTime()));
            params.put("enterTimeStrRight",
                    new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(queryAccessRecordBO.getEndTime()));
        }else if (queryAccessRecordBO.getStatus()==1){
            params.put("exitTimeStrLeft",
                    new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(queryAccessRecordBO.getStartTime()));
            params.put("exitTimeStrRight",
                    new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(queryAccessRecordBO.getEndTime()));
        }

        JSONObject resultObject = generalDaHuaIccHandler.getAndParams(url, params, vehicleIntegrationConfigBO.getVehicleSystemInfoBO().getProjectId(),
                vehicleIntegrationConfigBO.getVehicleSystemInfoBO().getProductId(), vehicleIntegrationConfigBO.getVehicleParkingInfoBO().getHost(),
                vehicleIntegrationConfigBO.getVehicleParkingInfoBO().getUser(), vehicleIntegrationConfigBO.getVehicleParkingInfoBO().getPassword(),
                vehicleIntegrationConfigBO.getVehicleParkingInfoBO().getAppId(), vehicleIntegrationConfigBO.getVehicleParkingInfoBO().getAppSecret());
        if (resultObject == null || !resultObject.getBoolean("success")) {
            log.error("post path:{};params:{};errMsg:{}", url, JSON.toJSONString(params), resultObject.getString("errMsg"));
            return null;
        }
        String data = resultObject.getString("data");
        JSONObject dataObject = JSON.parseObject(data);
        String pageDataStr = dataObject.getString("pageData");
        if (StringUtils.isEmpty(pageDataStr)) {
            log.error("post path:{};params:{};pageData is null!", DaHuaIccConstant.PEDESTRIAN_RECORD_URL, JSON.toJSONString(params));
            return null;
        }
        return JSONObject.parseArray(pageDataStr,VehicleInOutRecordDaHuaDTO.class);
    }


}
